<template>
    <div class="app-container" v-loading.fullscreen.lock="fullLoading">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学生姓名" prop="stuName">
                <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
                <el-select size="mini" v-model="queryParams.status" placeholder="审核状态" clearable>
                    <el-option v-for="(v, i) in applyStatus" :key="i" :label="v.label" :value="v.value" />
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button icon="el-icon-download" type="success" size="mini" @click="exportVClick">导出审核信息</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="table_data">
            <el-table-column label="学生" prop="stuName" align="center" />
            <el-table-column label="学号" prop="stuNo" align="center" />
            <el-table-column label="提交时间" prop="createTime" align="center" />
            <el-table-column label="申请状态" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.applyStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" icon="el-icon-view" @click="auditVClick(scope.row)">查看详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <el-dialog title="申请" :visible.sync="auditV" fullscreen :close-on-click-modal="false">
            <el-form class="detail" label-width="160px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级">
                            <el-input readonly size="mini" v-model="auditForm.className" class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名">
                            <el-input readonly size="mini" v-model="auditForm.stuName" class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="政治面貌">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.zzmm"></el-input>
                        </el-form-item>
                        <el-form-item label="职务">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.classPost"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.classTwo)" label="第二课堂学分认定">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.classTwo"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.youthStudy)" label="青年大学习学习率">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.youthStudy"></el-input>
                        </el-form-item>
                        <el-form-item label="获奖情况">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.goodHis"></el-input>
                        </el-form-item>
                        <el-form-item label="主要事迹">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.mainHis"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-table :data="rank_data">
                            <el-table-column label="学业成绩专业排名" align="center">
                                <template slot-scope="scope">
                                    {{ scope.row.majorRank + "/" + scope.row.majorCount }}
                                </template>
                            </el-table-column>
                            <el-table-column label="专业排名百分比(四舍五入保留整数)" align="center">
                                <template slot-scope="scope">
                                    {{ Math.round(Math.round(scope.row.majorRank / scope.row.majorCount * 10000) / 100)
                                        + "%" }}
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-table :data="sport_data">
                            <el-table-column label="学年" align="center" prop="stuYearName" />
                            <el-table-column label="体测成绩" align="center" prop="sportScore" />
                        </el-table>


                    </el-col>

                    <el-col :span="12">
                        <el-form-item v-if="!isEmpty(auditForm.fdyCmt)" label="辅导员审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.fdyCmt"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.xwCmt)" label="学院审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.xwCmt"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.jwcCmt)" label="学工审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.jwcCmt"></el-input>
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>


        </el-dialog>

        <el-dialog title="导出奖项审核结果" :visible.sync="exportV" width="500px">

            <el-select clearable placeholder="请选择学年" v-model="exportForm.stuYearId">
                <el-option v-for="(v, i) in year_list" :key="i" :value="v.id" :label="v.stuYearName"></el-option>
            </el-select>

            <el-select clearable style="width: 442px;" placeholder="请选择专业" v-model="exportForm.majorId">
                <el-option v-for="(v, i) in major_list" :key="i" :value="v.majorId" :label="v.majorName"></el-option>
            </el-select>
            <div style="font-size: small;margin: 10px 0 10px 10px; color: red;">*学年是必选项，班级不选就默认导出全部。</div>
            <el-button @click="doExport">导出</el-button>
        </el-dialog>

    </div>
</template>
<script>
import {
    applyStatus
} from "@/api/stuCQS/good/audit";
import { listOwnClass } from "@/api/stuCQS/info-fill/stu_eva_task";
import { listEnableYear as listYear } from "@/api/stuCQS/basedata/year";
import { listSelect } from "@/api/stuCQS/lake/type";
import { listXwAll, xwExport, getStuScoreMajorRank, listStuSportScore } from "@/api/comprehensive/biyeapply";
import { listOwnMajor } from "@/api/stuCQS/info-fill/cqScore";

import * as XLSX from 'xlsx';
import { workbook2blob, openDownloadDialog } from "@/api/helpFunc";
import lodash from "lodash";
import { isEmpty } from "@/api/helpFunc";


export default {
    name: "fdy-audit",
    dicts: ['audius_detail_type'],
    data() {
        return {
            applyStatus,

            isEmpty,

            fullLoading: false,

            loading: false,
            table_data: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                code: null,
                status: null,
                stuNo: "",
                stuName: ""
            },

            auditV: false,
            auditForm: {},
            material_list: [],

            score_list: [],
            myScoreV: false,
            course_list: [],
            courseParams: {
                pageNum: 1,
                pageSize: 10
            },
            courseTotal: 0,
            course_loading: false,

            count_list: [],

            iamV: false,
            iam_count: [],

            doForm: {
                auditId: null,
                status: null,
                common: ""
            },

            exportForm: {
                code: null,
                majorId: null,
                stuYearId: null
            },
            exportV: false,

            year_list: [],
            search_type_list: [],
            major_list: [],

            rank_data: [],
            sport_data: []

        }
    },
    created() {
        this.listSelect();
        this.listOwnMajor();
        this.listAllStuYear();
        this.getList();
    },
    methods: {
        async listOwnMajor() {
            let res = await listOwnMajor();
            if (res.code == 200) {
                this.major_list = [...res.data];
            }
        },
        async listSelect() {
            let res = await listSelect();
            if (res.code == 200) {
                this.search_type_list = [...res.data];
            }
        },
        async listAllStuYear() {
            let res = await listYear();
            if (res.code == 200) {
                this.year_list = [...res.rows];
            }
        },
        async listOwnClass() {
            let res = await listOwnClass();
            if (res.code == 200) {
                this.class_list = [...res.data];
            }
        },
        async doExport() {
            let sdata = { ...this.exportForm };
            if (isEmpty(sdata.stuYearId)) {
                this.$message.info("请选择学年");
                return;
            }

            this.loading = true;
            let res = await xwExport(sdata);
            this.loading = false;
            if (res.code == 200) {
                let fileName = "";
                let fileHeader = [];
                let fileHeaderZh = {};
                let fileCol = [];
                fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'stuMajorRank', 'stuMajorPer', 'applyStatusEx', 'goodHis', 'mainHis', 'importTime', 'teacherName'];

                let temp = [...res.data];
                let data = [];
                temp.map(x => {
                    let needAdd = {};
                    fileHeader.map(y => {
                        switch (y) {
                            case "stuMajorRank":
                                needAdd[y] = x[y] + "/" + x["majorCount"];
                                break;
                            case "stuMajorPer":
                                needAdd[y] = Math.round(Math.round(x.stuMajorRank / x.majorCount * 10000) / 100) + "%";
                                break;
                            default:
                                needAdd[y] = x[y];
                        }
                    });
                    data.push(needAdd);
                })

                fileHeaderZh = {
                    'stuYearName': "学年",
                    "deptName": "学院",
                    "majorName": "专业",
                    "className": "班级",
                    "stuNo": "学号",
                    "stuName": "姓名",
                    "stuMajorRank": "成绩专业排名",
                    "stuMajorPer": "成绩专业排名百分比",
                    "applyStatusEx": "审核状态",
                    "goodHis": "获奖记录",
                    "mainHis": "主要事迹",
                    "importTime": "导出时间",
                    'teacherName': "辅导员"
                };

                fileCol = [
                    { wch: 15 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 10 },
                    { wch: 10 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 20 },
                ];
                fileName = "优秀毕业生名单";

                const arrayWithHeader = [fileHeaderZh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: fileHeader,
                        skipHeader: true
                    });
                sheet1["!cols"] = fileCol;

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, fileName);
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, fileName + ".xlsx");  // 辅助函数openDownloadDialog

            } else {
                this.$message.info("出错啦，刷新试试");
            }

        },
        async exportVClick() {
            this.exportV = true;
        },
        async getList() {
            this.loading = true;
            let res = await listXwAll(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.table_data = [...res.rows];
                this.total = res.total;
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async auditVClick(v) {
            this.auditForm = { ...v };
            this.auditV = true;
            this.getStuScoreMajorRank(v.stuNo);
            this.listStuSportScore(v.stuNo);
        },
        async getStuScoreMajorRank(stuNo) {
            let res = await getStuScoreMajorRank(stuNo);
            if (res.code == 200) {
                this.rank_data = [res.data];
            }
        },
        async listStuSportScore(stuNo) {
            let res = await listStuSportScore(stuNo);
            if (res.code == 200) {
                this.sport_data = [...res.data];
            }
        }
    }
}

</script>
<style lang="scss" scoped>
.addForm-input {
    width: 400px;
}

.detail {
    .el-form-item {
        margin-bottom: 0px;
    }
}
</style>